package com.hkts.naturalgas.dictionaries.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionDictionaries;
import com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionItem;
import com.hkts.naturalgas.dictionaries.mapper.HktsEnvironmentProtectionItemMapper;
import com.hkts.naturalgas.dictionaries.service.IHktsEnvironmentProtectionItemService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-06-23
 */
@Service
@Transactional
public class HktsEnvironmentProtectionItemServiceImpl extends ServiceImpl<HktsEnvironmentProtectionItemMapper, HktsEnvironmentProtectionItem> implements IHktsEnvironmentProtectionItemService {


    @Autowired
    private HktsEnvironmentProtectionItemMapper hktsEnvironmentProtectionItemMapper;

    @Override
    public IPage<HktsEnvironmentProtectionItem> selectPage(Page<HktsEnvironmentProtectionItem> page, HktsEnvironmentProtectionItem hktsEnvironmentProtectionItem) {
        QueryWrapper<HktsEnvironmentProtectionItem> query = Wrappers.query();
        QueryWrapper<HktsEnvironmentProtectionItem> entityWrapper = getEntityWrapper(query, hktsEnvironmentProtectionItem);
        return hktsEnvironmentProtectionItemMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HktsEnvironmentProtectionItem> selectList(HktsEnvironmentProtectionItem hktsEnvironmentProtectionItem) {
        QueryWrapper<HktsEnvironmentProtectionItem> query = Wrappers.query();
        QueryWrapper<HktsEnvironmentProtectionItem> entityWrapper = getEntityWrapper(query, hktsEnvironmentProtectionItem);
        entityWrapper.orderByAsc(HktsEnvironmentProtectionItem.FILED_ORDER);
        return hktsEnvironmentProtectionItemMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsEnvironmentProtectionItem> getEntityWrapper(QueryWrapper<HktsEnvironmentProtectionItem> entityWrapper, HktsEnvironmentProtectionItem hktsEnvironmentProtectionItem) {
        //条件拼接
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionItem.getFiledDescribe())) {
            entityWrapper.eq(HktsEnvironmentProtectionItem.FILED_DESCRIBE, hktsEnvironmentProtectionItem.getFiledDescribe());
        }
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionItem.getFieldName())) {
            entityWrapper.eq(HktsEnvironmentProtectionItem.FIELD_NAME, hktsEnvironmentProtectionItem.getFieldName());
        }
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionItem.getDataType())) {
            entityWrapper.eq(HktsEnvironmentProtectionItem.DATA_TYPE, hktsEnvironmentProtectionItem.getDataType());
        }

        if (StringUtils.isNotBlank(hktsEnvironmentProtectionItem.getCorrespondingField())) {
            entityWrapper.eq(HktsEnvironmentProtectionItem.CORRESPONDING_FIELD, hktsEnvironmentProtectionItem.getCorrespondingField());
        }
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionItem.getIsExport())) {
            entityWrapper.eq(HktsEnvironmentProtectionItem.IS_EXPORT, hktsEnvironmentProtectionItem.getIsExport());
        }
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionItem.getIsDefault())) {
            entityWrapper.eq(HktsEnvironmentProtectionItem.IS_DEFAULT, hktsEnvironmentProtectionItem.getIsDefault());
        }
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionItem.getIsShow())) {
            entityWrapper.eq(HktsEnvironmentProtectionItem.IS_SHOW, hktsEnvironmentProtectionItem.getIsShow());
        }
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionItem.getIsEnable())) {
            entityWrapper.eq(HktsEnvironmentProtectionItem.IS_ENABLE, hktsEnvironmentProtectionItem.getIsEnable());
        }
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionItem.getTableId())) {
            entityWrapper.eq(HktsEnvironmentProtectionItem.TABLE_ID, hktsEnvironmentProtectionItem.getTableId());
        }
        return entityWrapper;
    }

    @Override
    public List<HktsEnvironmentProtectionItem> getDbTable() {
        QueryWrapper<HktsEnvironmentProtectionItem> query = Wrappers.query();
        query.lambda().eq(HktsEnvironmentProtectionItem::getTableId, 1);
        List<HktsEnvironmentProtectionItem> hktsEnvironmentProtectionItem = hktsEnvironmentProtectionItemMapper.selectList(query);
        return hktsEnvironmentProtectionItem;
    }
}
